package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/courses")
@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result list(String name, Integer subject, Integer target,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("列表查询: name:{}, subject:{}, target:{}, page:{}, pageSize:{}", name, subject, target, page, pageSize);
        // 调用service,分页查询
        PageResult<Course> pageResult = courseService.list(name, subject, target, page, pageSize);
        return Result.success(pageResult);

    }

    @PostMapping
    public Result add(@RequestBody Course course) {
        log.info("添加: {}", course);
        courseService.add(course);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除: {}", id);
        courseService.delete(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Course course) {
        log.info("修改: {}", course);
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询课程ID:{}", id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }
    @GetMapping("/list")
    public Result findList() {
        log.info("查询所有课程");
        List<Course> list = courseService.findList();
        return Result.success(list);
    }
    @GetMapping("/subject/{subject}")
    public Result findBySubject(@PathVariable Integer subject) {
        log.info("查询课程:{}", subject);
        List<Course> list = courseService.findBySubject(subject);
        return Result.success( list);
    }

}